# -*- coding: utf-8 -*-
import shelve

# 数据预处理
def preprocess():
    inFile = open( "trainSetTemp.txt" )
    data = shelve.open( "data.db", writeback = True )
    last_user_id = '0'
    sum = 0
    x = 1
    for line in ( line.split("::") for line in inFile.readlines() ):
        attributeMap = {}
        attributeMap[ 'userid' ] = line[ 0 ]
        attributeMap[ 'movieid' ] = line[ 1 ]
        attributeMap[ 'comment' ] = line[ 2 ]
        attributeMap[ 'time' ] = line[ 3 ]
        attributeMap[ 'score' ] = line[ 4 ].strip()

        if data.has_key( attributeMap[ 'userid' ] ) == False:
            data[ attributeMap[ 'userid' ] ] = dict()
            
        data[ attributeMap[ 'userid' ] ][ attributeMap[ 'movieid' ] ] = attributeMap
        if last_user_id != attributeMap[ 'userid' ]:
            data[ attributeMap[ 'userid' ] ][ 'user_mean' ] = sum * 1.0 / x
            #print sum * 1.0 / x
            sum = float( attributeMap[ 'score' ] )
            x = 1
            last_user_id = attributeMap[ 'userid' ]
            continue
        sum += float( attributeMap[ 'score' ] )
        x += 1
        
    data.close()
    inFile.close()

# 打印处理好的数据     
def print_data():
    data = shelve.open( "data.db" )
    for user_id in data:
        for movie_id in data[ user_id ]:
            print data[ user_id ][ movie_id ]
        print
            
        

# 相似函数
def sim( x, y ):
    for xx in x:
        pass
    
if __name__ == "__main__":
    preprocess()
    print "preprocess done."
    #print_data()
